home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / _read.c < prev    next >
C/C++ Source or Header  |  1994-04-04  |  2KB  |  109 lines

  1. RCS_ID_C="$Id: _read.c,v 1.4 1994/04/04 01:27:59 jraja Exp $";
  2. /*
  3.  * _read.c --- read() for both files and sockets
  4.  *
  5.  * Author: jraja <Jarno.Rajahalme@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP Network Support Library.
  8.  *
  9.  * Copyright © 1994 AmiTCP/IP Group, <amitcp-group@hut.fi>
  10.  *                  Helsinki University of Technology, Finland.
  11.  *                  All rights reserved.
  12.  *
  13.  * Created      : Tue Mar  8 21:15:50 1994 jraja
  14.  * Last modified: Wed Mar 30 10:22:38 1994 jraja
  15.  *
  16.  */
  17.  
  18. #include <ios1.h>
  19. #include <stdlib.h>
  20. #include <dos.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <dos/dos.h>
  24. #include <proto/dos.h>
  25.  
  26. #include <bsdsocket.h>
  27.  
  28. extern int __io2errno(long);
  29.  
  30. int
  31. __read(int fd, void *buffer, unsigned int length)
  32. {
  33.   struct UFB *ufb;
  34.   int         count;
  35.   char        ch, *ptr, *nextptr, *endptr;
  36.  
  37.   /*
  38.    * Check for the break signals
  39.    */
  40.   __chkabort();
  41.   /*
  42.    * find the ufb *
  43.    */
  44.   if ((ufb = __chkufb(fd)) == NULL) {
  45.     errno = EINVAL;
  46.     return -1;
  47.   }
  48.   /*
  49.    * Check if read is allowed
  50.    */
  51.   if (!(ufb->ufbflg & UFB_RA)) {
  52.     _OSERR = ERROR_READ_PROTECTED;
  53.     errno = EIO;
  54.     return -1;
  55.   }
  56.  
  57.   /*
  58.    * Do the Actual read
  59.    */
  60.   _OSERR = 0;
  61.   if (ufb->ufbflg & UFB_SOCK) {
  62.     if ((count = recv(fd, (UBYTE *)buffer, length, 0)) < 0) {
  63.       return -1;
  64.     }
  65.   }
  66.   else {
  67.     if ((count = Read(ufb->ufbfh, buffer, length)) == -1) {
  68.       errno = __io2errno(_OSERR = IoErr());
  69.       return -1;
  70.     }
  71.   }
  72.   /*
  73.    * Check if translation is not needed
  74.    */
  75.   if (count == 0 || !(ufb->ufbflg & UFB_XLAT))
  76.     return count;
  77.   
  78.   endptr = (char *)buffer + count;    /* first point NOT in buffer */
  79.   
  80.   if (endptr[-1] == 0x0D)    /* checks last char */
  81. #if 0
  82.     Read(ufb->ufbfh, buffer, 1); /* overwrites first read byte ! */
  83.   /*            BUG  ^^^^^^ */
  84. #else
  85.     count--, endptr--;
  86. #endif
  87.  
  88.   /*
  89.    * Remove 0x0D's (CR) (This doesn't remove a CR at the end of the buffer).
  90.    */
  91.   if ((ptr = memchr(buffer, 0x0D, count)) != NULL) {
  92.     nextptr = ptr + 1;
  93.     while (nextptr < endptr) {
  94.       if ((ch = *nextptr) != 0x0D)
  95.     *ptr++ = ch;
  96.       nextptr++;
  97.     }
  98.     count = ptr - (char *)buffer;
  99.   }
  100.  
  101.   /*
  102.    * Test for CTRL-Z (end of file)
  103.    */
  104.   if ((ptr = memchr(buffer, 0x1A, count)) == NULL)
  105.     return count;
  106.  
  107.   return ptr - (char *)buffer;
  108. }
  109.